<!-- 小剧场底部操作拦 -->
<template>
  <div class="bottom-bar">
    <div class="top-trip-list" v-if="buttonStatus && recommendReply.length > 0">
      <div class="top-trip-item" v-for="item in recommendReply" @click="sendMessage(item as string)">{{ item }}</div>
    </div>
    <div class="main">
      <div ref="messageBar" class="message-bar" v-if="buttonStatus">
        <van-field
          autocomplete="off"
          ref="messageInputRef"
          v-model="message"
          class="message-input"
          placeholder="Say something to get someone to like you"
          :maxlength="500"
          @focus="handleFocus"
          @blur="handleBlur"
          @keyup.enter="() => sendMessage()"
        />
        <div class="message-sent buttonClick" @click="sendMessage()" :class="{ 'button-list-active': message.length > 0 }"></div>
      </div>

      <div class="button-list" v-else>
        <div class="download-button buttonClick" v-if="!buttonStatus" @click="open_bot_app(aiUserId)">Download</div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { open_bot_app } from '@/utils/download'
import { sendMsg } from '@/api/chat'
import { isAndroid } from '@/utils/navigator'

const messageInputRef = ref<HTMLInputElement>() // 消息输入框组件
const messageBar = ref<HTMLDivElement>() // 底部的弹窗
const message = ref<string>('') // 当前输入的消息
const emit = defineEmits(['sendMsg', 'sendMsgLoding', 'inputFocus', 'inputAndroidFocus', 'inputBlur'])
const props = defineProps({
  // 状态
  buttonStatus: {
    type: Boolean,
    default: true,
  },
  aiUserId: {
    type: Number,
    default: 0,
  },
  // 推荐回复
  recommendReply: {
    type: Array,
    default: () => {
      return []
    },
  },
})

const inputFocus = () => {
  messageInputRef.value?.blur()
}
// 当input获取焦点后，如果滚动了页面，就让input失去焦点
const handleFocus = () => {
  window.addEventListener('touchmove', inputFocus, false)
  // 部分安卓浏览器需要让输入框滚动到可见区域
  if (isAndroid) {
    // emit('inputFocus')
    // 无法预测每台手机的键盘弹起时间
    setTimeout(() => {
      messageBar.value?.scrollIntoView(true)
      emit('inputAndroidFocus')
    }, 100)
    setTimeout(() => {
      messageBar.value?.scrollIntoView(true)
      emit('inputAndroidFocus')
    }, 200)
    setTimeout(() => {
      messageBar.value?.scrollIntoView(true)
      emit('inputAndroidFocus')
    }, 300)
    setTimeout(() => {
      emit('inputAndroidFocus')
    }, 400)
  } else {
    emit('inputFocus')
  }
}
// 失去焦点后，删除一下时间挂载
const handleBlur = () => {
  window.removeEventListener('touchmove', inputFocus, false)
  if (!isAndroid) {
    emit('inputBlur')
  }
}
// 发送消息
const sendMessage = (text?: string) => {
  if (!message.value && !text) return
  const accessId = Math.ceil(Math.random() * 100000000)
  const content = message.value || text || ''
  emit('sendMsgLoding', {
    content: content,
    MsgID: accessId,
    accessId,
  })
  message.value = ''
  sendMsg(content, props.aiUserId)
    .then(res => {
      // 在本地添加一条数据
      const { MsgID } = res
      emit('sendMsg', {
        content: content,
        MsgID,
        accessId,
      })
    })
    .catch(console.error)
}
</script>

<style scoped lang="scss">
.bottom-bar {
  position: sticky;
  bottom: 0;
  left: 0;
  text-align: center;
  z-index: 2;
  .main {
    background-color: rgba(51, 51, 51, 1);
  }
  .top-trip {
    display: flex;
    padding: 10px 0;
    .rule-button {
      height: 24px;
      padding: 0 9px;
      color: #000;
      text-align: center;
      font-family: 'PingFang SC';
      font-size: 14px;
      font-style: normal;
      font-weight: 400;
      line-height: 24px;
      letter-spacing: -0.667px;
      border-radius: 12px;
      margin-right: 6px;
      background-color: #fedba3;
    }
  }
  .message-bar {
    height: 40px;
    background-color: rgba(51, 51, 51, 1);
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 10px 8px 14px;
    .message-input {
      height: 40px;
      background-color: rgba(51, 51, 51, 1);
      flex: 1;
      padding: 0;
      line-height: 40px;
      border-radius: 14px;
      border: 1px solid rgba(133, 133, 133, 1);
      padding: 0 12px;
      :deep(input) {
        color: #fff;
      }
    }
    :deep(.van-cell::after) {
      display: none;
    }
    .message-sent {
      width: 46px;
      height: 40px;
      background: url('@/assets/images/send-not-bot.png') no-repeat 16px center;
      background-size: 28px 28px;
      &.button-list-active {
        background: url('@/assets/images/send-bot.png') no-repeat 16px center;
        background-size: 28px 28px;
      }
    }
  }
  .button-list {
    display: flex;
    justify-content: space-between;
    padding: 10px 16px;
    .download-button {
      height: 44px;
      line-height: 44px;
      background-image: linear-gradient(115.62deg, #9259fe 16.21%, #6365ff 83.79%);
      color: #fff;
      font-family: SF Pro Text;
      font-size: 16px;
      font-weight: 700;
      text-align: center;
      width: 100%;
      border-radius: 14px;
    }
  }
}

.buttonClick {
  position: relative;
}

.top-trip-list {
  display: flex;
  padding: 12px 16px 12px;
  gap: 8px;
  overflow-x: scroll;
  overflow-y: hidden;
  .top-trip-item {
    height: 32px;
    line-height: 32px;
    padding: 0px 8px;
    border-radius: 16px;
    background-color: rgba(106, 69, 255, 0.5);
    font-family: SF UI Text;
    font-size: 16px;
    font-weight: 500;
    text-align: left;
    width: fit-content;
    color: rgba(255, 255, 255, 1);
    white-space: nowrap;
  }
}
</style>
